package me.xiaolei.andlog.interceptor.impls

import me.xiaolei.andlog.comm.InterceptorChain
import me.xiaolei.andlog.comm.LogType
import android.util.Log
import me.xiaolei.andlog.interceptor.IInterceptor

/**
 * 使用打印API进行输出日志
 */
object LogInterceptor : IInterceptor
{
    // 是否启用
    var enable: Boolean = true

    // 过滤只输出某种类型的日志
    var filterLogType: LogType? = null

    override fun intercept(chain: InterceptorChain)
    {
        val filterType = filterLogType
        val type = chain.type
        val tag = chain.tag
        val msg = chain.msg
        try
        {
            if (!enable || (filterType != null && filterType != type))
                return
            when (type)
            {
                LogType.VERBOSE -> Log.v(tag, msg)
                LogType.DEBUG -> Log.d(tag, msg)
                LogType.INFO -> Log.i(tag, msg)
                LogType.WARNING -> Log.w(tag, msg)
                LogType.ERROR -> Log.e(tag, msg)
            }
        } finally
        {
            chain.proceed()
        }
    }
}